package com.ruoyi.web.api.base;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.UserVo;
import com.ruoyi.common.exception.TokenGetUserException;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

/**
 * @Auther: lsq
 * @Date: 2023/10/17
 * @Description:
 */
public class ApiBaseController extends BaseController {


    public UserVo getLoginUserInfo() {
        String token = getRequest().getHeader("token");
        if (StringUtils.isBlank(token))
        {
            throw new TokenGetUserException("缺少token参数！");
        }
        RedisService bean = SpringUtils.getBean(RedisService.class);
        JSONObject result = bean.getCacheObject(token);
        if (result == null)
        {
            throw new TokenGetUserException("根据token未获取到用户信息！");
        }
        UserVo userInfo = result.toJavaObject(UserVo.class);
        return userInfo;
    }

    public String getToken() {
        return getRequest().getHeader("token");
    }
}
